package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.ElevatorBrand;
import com.gc.business.service.IElevatorBrandService;
import com.gc.common.base.Result;
import com.gc.common.constant.Constants;
import com.gc.common.support.Convert;
import com.gc.framework.util.ShiroUtils;
import com.gc.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.ElevatorModel;
import com.gc.business.service.IElevatorModelService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯型号 控制层
 * 
 * @author 刘锦腾
 * @date 2020-12-08
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorModel")
public class ElevatorModelController extends BaseController {
    private String prefix = "business/elevatorModel";
	
	private final IElevatorModelService elevatorModelService;

	private final IElevatorBrandService elevatorBrandService;

	@Autowired
	private ISysUserService userService;

	@Autowired
	public ElevatorModelController(IElevatorModelService elevatorModelService,
								   IElevatorBrandService elevatorBrandService) {
		this.elevatorModelService = elevatorModelService;
		this.elevatorBrandService = elevatorBrandService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:elevatorModel:view")
	@GetMapping
	public String elevatorModel(){
	    return prefix + "/elevatorModel";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(ModelMap modelMap){
		List<ElevatorBrand> elevatorBrandList = elevatorBrandService.list(new ElevatorBrand());
		modelMap.put("elevatorBrandList",elevatorBrandList);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{modelId}")
	public String edit(@PathVariable("modelId") Long modelId, ModelMap modelMap){
		List<ElevatorBrand> elevatorBrandList = elevatorBrandService.list(new ElevatorBrand());
		modelMap.put("elevatorBrandList",elevatorBrandList);
		ElevatorModel elevatorModel = elevatorModelService.getById(modelId);
		modelMap.put("elevatorModel", elevatorModel);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯型号列表
	 */
	@RequiresPermissions("business:elevatorModel:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(ElevatorModel elevatorModel){

		Long attrId = getAttrId();
		if(userService.ifCompanyRole(attrId)){
			elevatorModel.setCompanyId(attrId);
			startPage();
			List<ElevatorModel> list = elevatorModelService.list(elevatorModel);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<ElevatorModel>()));
		}
	}

	/**
	 * 新增保存电梯型号
	 */
	@RequiresPermissions("business:elevatorModel:add")
	@Log(title = "电梯型号", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ElevatorModel elevatorModel){
		elevatorModel.setCompanyId(getAttrId());
		elevatorModel.setCreateBy(ShiroUtils.getLoginName());
		elevatorModel.setCreateTime(DateUtil.date());
		if (elevatorModel.getBrandId() == null){
			return Result.failed("请选择电梯品牌");
		}
		ElevatorBrand elevatorBrand = elevatorBrandService.getById(elevatorModel.getBrandId());
		if (elevatorBrand == null){
			return Result.failed("该品牌不存在");
		}
		elevatorModel.setBrandName(elevatorBrand.getBrandName());
		elevatorModelService.save(elevatorModel);
		return Result.success();
	}

	/**
	 * 修改保存电梯型号
	 */
	@RequiresPermissions("business:elevatorModel:edit")
	@Log(title = "电梯型号", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ElevatorModel elevatorModel){
		if (elevatorModel.getBrandId() == null){
			return Result.failed("请选择电梯品牌");
		}
		ElevatorBrand elevatorBrand = elevatorBrandService.getById(elevatorModel.getBrandId());
		if (elevatorBrand == null){
			return Result.failed("该品牌不存在");
		}
		elevatorModel.setBrandName(elevatorBrand.getBrandName());
		elevatorModel.setUpdateBy(ShiroUtils.getLoginName());
		elevatorModel.setUpdateTime(DateUtil.date());
		elevatorModelService.updateById(elevatorModel);
		return Result.success();
	}
	
	/**
	 * 删除电梯型号
	 */
	@RequiresPermissions("business:elevatorModel:remove")
	@Log(title = "电梯型号", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		elevatorModelService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯型号列表
	 */
	@RequiresPermissions("business:elevatorModel:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ElevatorModel elevatorModel){
		List<ElevatorModel> list = elevatorModelService.list(elevatorModel);
		ExcelUtil<ElevatorModel> util = new ExcelUtil<>(ElevatorModel.class);
		util.exportExcel(list, "elevatorModel");
		return Result.success();
	}
}
